package com.sheng.leetcode.year2023.month06.day02;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author liusheng
 * @date 2023/06/02
 * <p>
 * 2559. 统计范围内的元音字符串数<p>
 * <p>
 * 给你一个下标从 0 开始的字符串数组 words 以及一个二维整数数组 queries 。<p>
 * 每个查询 queries[i] = [li, ri] 会要求我们统计在 words 中下标<p>
 * 在 li 到 ri 范围内（包含 这两个值）并且以元音开头和结尾的字符串的数目。<p>
 * 返回一个整数数组，其中数组的第 i 个元素对应第 i 个查询的答案。<p>
 * 注意：元音字母是 'a'、'e'、'i'、'o' 和 'u' 。<p>
 * <p>
 * 示例 1：<p>
 * 输入：words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]<p>
 * 输出：[2,3,0]<p>
 * 解释：以元音开头和结尾的字符串是 "aba"、"ece"、"aa" 和 "e" 。<p>
 * 查询 [0,2] 结果为 2（字符串 "aba" 和 "ece"）。<p>
 * 查询 [1,4] 结果为 3（字符串 "ece"、"aa"、"e"）。<p>
 * 查询 [1,1] 结果为 0 。<p>
 * 返回结果 [2,3,0] 。<p>
 * <p>
 * 示例 2：<p>
 * 输入：words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]<p>
 * 输出：[3,2,1]<p>
 * 解释：每个字符串都满足这一条件，所以返回 [3,2,1] 。<p>
 * <p>
 * 提示：<p>
 * 1 <= words.length <= 10^5<p>
 * 1 <= words[i].length <= 40<p>
 * words[i] 仅由小写英文字母组成<p>
 * sum(words[i].length) <= 3 * 10^5<p>
 * 1 <= queries.length <= 10^5<p>
 * 0 <= queries[j][0] <= queries[j][1] < words.length<p>
 * <p>
 * 来源：力扣（LeetCode）<p>
 * 链接：<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges">2559. 统计范围内的元音字符串数</a><p>
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。<p>
 */
public class LeetCode2559 {

    @Test
    public void test01() {
//        String[] words = {"aba", "bcb", "ece", "aa", "e"};
//        int[][] queries = {{0, 2}, {1, 4}, {1, 1}};
        String[] words = {"a", "e", "i"};
        int[][] queries = {{0, 2}, {0, 1}, {2, 2}};
        System.out.println(Arrays.toString(new Solution().vowelStrings(words, queries)));
    }
}

class Solution {
    public int[] vowelStrings(String[] words, int[][] queries) {
        List<Character> list = Arrays.asList('a', 'e', 'i', 'o', 'u');
        int n = queries.length;
        int[] nums = new int[n];
        int[] ints = new int[words.length + 1];
        for (int i = 0; i < words.length; i++) {
            int length = words[i].length();
            ints[i + 1] = ints[i] + (list.contains(words[i].charAt(0)) && list.contains(words[i].charAt(length - 1)) ? 1 : 0);
        }
        for (int i = 0; i < n; i++) {
            int li = queries[i][0];
            int ri = queries[i][1];
            nums[i] = ints[ri + 1] - ints[li];
        }
        return nums;
    }
}
